import withAuth from '../../../helpers/basicAuth';
import {
    getDescriptionListGroup,
    getInputByLabel,
    getSelectOption,
} from '../../../helpers/formHelpers';
import {
    getWizardNavStep,
    getWizardStepTitle,
    goToWizardStep,
    navigateWizardBack,
    navigateWizardNext,
} from '../../../helpers/wizard';
import { tryDeleteCollection } from '../../collections/Collections.helpers';
import {
    clickCreateReport,
    tryDeleteReportConfiguration,
    visitVulnerabilityReports,
} from './VulnerabilityReporting.helpers';

describe('Vulnerability Reporting form', () => {
    const testReportName = 'CYPRESS-VM-REPORT-NAME';
    const testCollectionName = 'CYPRESS-VM-REPORT-COLLECTION';

    const wizardStep1 = 'Configure report parameters';
    const wizardStep2 = 'Configure delivery destinations';
    const wizardStep3 = 'Review';

    withAuth();

    beforeEach(() => {
        tryDeleteReportConfiguration(testReportName).then(() => {
            tryDeleteCollection(testCollectionName);
        });
    });

    afterEach(() => {
        tryDeleteReportConfiguration(testReportName).then(() => {
            tryDeleteCollection(testCollectionName);
        });
    });

    function assertWizardNavigationEnabled(isEnabled: boolean) {
        const assertion = isEnabled ? 'be.enabled' : 'be.disabled';
        cy.get('button:contains("Next")').should(assertion);
        getWizardNavStep(wizardStep2).should(assertion);
        getWizardNavStep(wizardStep3).should(assertion);
    }

    function createCollectionForReportConfigViaModal() {
        getInputByLabel('Configure collection included').click();

        cy.get('button:contains("Create collection")').first().click();
        getInputByLabel('Name').type(testCollectionName);
        getInputByLabel('Description').type('All deployments');

        cy.get('button:contains("No clusters specified")').click();
        cy.get('button:contains("Clusters with names matching")').click();
        cy.get('button:contains("An exact value of")').click();
        cy.get('ul li:contains("A regex value of")').click();
        cy.get('input[aria-label="Select value 1 of 1 for the cluster name"]').type('.*');
        cy.get('button:contains("Save")').click();
    }

    function validateReportParameters(params: [string, string][]) {
        params.forEach(([key, value]) => {
            getDescriptionListGroup(key, value);
        });
    }

    it('should handle navigation throughout the report configuration form wizard', () => {
        visitVulnerabilityReports();
        clickCreateReport();

        // Check Wizard state
        // TODO "Next" button and wizard nav links should be disabled here, as required fields are not yet filled
        // assertWizardNavigationEnabled(false);

        // Set name
        getInputByLabel('Report name').type(testReportName);

        // Navigation becomes disabled due to a collection not being selected
        assertWizardNavigationEnabled(false);

        // Create a Collection via the modal
        createCollectionForReportConfigViaModal();

        // Creation of a Collection is the final required field at this point, so all wizard nav should be enabled
        assertWizardNavigationEnabled(true);

        // Navigate to step 2 and step 3, and then back via the footer buttons
        getWizardStepTitle(wizardStep1);
        navigateWizardNext();
        getWizardStepTitle(wizardStep2);
        navigateWizardNext();
        getWizardStepTitle(wizardStep3);
        navigateWizardBack();
        getWizardStepTitle(wizardStep2);
        navigateWizardBack();
        getWizardStepTitle(wizardStep1);

        // Test navigation via wizard navigation
        goToWizardStep(2);
        getWizardStepTitle(wizardStep2);
        goToWizardStep(3);
        getWizardStepTitle(wizardStep3);
        // Back to the beginning
        goToWizardStep(1);
        getWizardStepTitle(wizardStep1);
    });

    it('creates a report configuration via the wizard', () => {
        const expectedReportParameters: [string, string][] = [
            ['Report name', testReportName],
            ['Report description', ''],
            ['CVE severity', 'Critical'],
            ['CVE severity', 'Important'],
            ['CVE severity', 'Moderate'],
            ['CVE status', 'Unfixable'],
            ['Collection included', testCollectionName],
            ['Image type', 'Deployed images'],
            ['Image type', 'Watched images'],
            ['CVEs discovered since', 'All time'],
        ];
        visitVulnerabilityReports();
        clickCreateReport();

        // Set name
        getInputByLabel('Report name').type(testReportName);

        getInputByLabel('CVE severity').click();
        getSelectOption('Moderate').click();
        getInputByLabel('CVE severity').click();

        getInputByLabel('CVE status').click();
        getSelectOption('Unfixable').click();
        getInputByLabel('CVE status').click();

        // Create a Collection via the modal
        createCollectionForReportConfigViaModal();

        navigateWizardNext();
        // TODO Skip delivery destinations step for now
        navigateWizardNext();

        // Verify preview results
        validateReportParameters(expectedReportParameters);

        cy.get('button:contains("Save")').click();

        cy.get(`a:contains("${testReportName}")`).click();

        // Verify persisted results
        validateReportParameters(expectedReportParameters);
    });
});
